package cn.netjava.robo3d.device;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;

import cn.netjava.robo3d.TankInformation;

import com.jme.input.InputHandler;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jmex.awt.swingui.JMEDesktop;

/**
 * 我的作战地图
 * @author Administrator
 *
 */
public class Atlas extends Node {

	private DisplaySystem display;
	private JMEDesktop desktop;
	private JDesktopPane desktopPane;
    private float atlaswidth;
    private float atlasheight;
    private List<TankInformation> alltanksinfo;       //所有坦克属性
    private List<TankInformation> enemylist;          //敌人队列
    private Vector3f playerloc;          //角色坦克位置
    private TankInformation playerinfo;             //角色坦克
    private Mylabelinatlas melabel;                //中心指向标
    private float theta;              //我偏转的角度

	/**
	 * 作战地图构造器
	 * @param statNode  和graher有关的节点
	 * @param display   显示器对象
	 */
	public Atlas(InputHandler input, List<TankInformation> alltanksinfo){
		  display=DisplaySystem.getDisplaySystem();
		  this.alltanksinfo=alltanksinfo;
		  float displaywidth=display.getWidth();
		  float displayheight=display.getHeight();
		  desktop=new JMEDesktop("atlasdesk");
		  desktop.getJDesktop().setBackground(new Color(0.1f,0.9f,0.1f,0.2f));
		  desktop.setup(display.getWidth(), display.getHeight(), false, input);
		  desktop.setLightCombineMode(Spatial.LightCombineMode.Off);
		  atlaswidth=desktop.getWidth();
		  atlasheight=desktop.getHeight();
          this.attachChild(desktop);
		  this.setRenderQueueMode(Renderer.QUEUE_ORTHO);//这个设置让挡住的得以看见
		  this.setCullHint(Spatial.CullHint.Never);
		  this.setLocalScale(0.4f);
		  this.setLocalTranslation(displaywidth*(3/4f), displayheight*(3/4f), 0);
		  createswing();
	}

	private Vector3f relativevec=new Vector3f();
	public void update(){
		  this.setCullHint(Spatial.CullHint.Never);

		  playerloc=playerinfo.getLocaltranslation();

		  //更新敌方坦克的位置
		  //先不考虑坦克的丢弃，统统加进来
		  for(int i=0;i<enemylist.size();i++)
		  {
			  relativevec=enemylist.get(i).getLocaltranslation().subtract(playerloc);
			  relativevec.multLocal(3f);         //缩放
			  int tempX=(int)(atlaswidth/2)+(int)relativevec.x;
		      int tempY=(int)(atlasheight/2)+(int)relativevec.z;
		      enemylist.get(i).getLabelinatalas().setLocation(tempX, tempY);
		  }
		  //炮塔和X轴夹角
		  //theta=playerinfo.getGundirection().angleBetween(new Vector3f(1,0,0));
		  float dotproductX=playerinfo.getCardirection().dot(new Vector3f(1,0,0));
          float dotproductZ=playerinfo.getCardirection().dot(new Vector3f(0,0,1));
		  if(dotproductZ>0)
          {
		     theta=FastMath.acos(dotproductX);
          }else{
        	 theta=2*FastMath.PI-FastMath.acos(dotproductX);
          }
//		  float theta2=theta*FastMath.RAD_TO_DEG;
//		  System.out.println("theta2:"+theta2);
          melabel.update(theta);

	}

	private void createswing(){
		desktopPane=desktop.getJDesktop();
//		JInternalFrame inframe=new JInternalFrame("myframe");
//		inframe.setResizable(true);
//		inframe.setLocation(800,100);
//		inframe.setSize(200, 100);
//		JButton button=new JButton("测试按钮test_button");
//		button.setSize(100, 60);
//		button.setLocation(800, 500);
//		desktopPane.add(button);
//		desktopPane.add(inframe);
//		inframe.setVisible(true);
		//创建地图底板
		JLabel label1=new JLabel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.BLUE);
                g2d.drawLine(0, (int)atlasheight/2, (int)atlaswidth, (int)atlasheight/2);
                g2d.drawLine((int)atlaswidth/2, 0, (int)atlaswidth/2, (int)atlasheight);
                //画圆
                int ovalnum=10;  //圈数
                int step=(int)atlasheight/(2*ovalnum);
                int R2=(int)atlasheight;
                int ox=(int)(atlaswidth-atlasheight)/2;
                int oy=0;  //原点
                for(int i=0;i<ovalnum;i++)
                {
                    g2d.drawOval(ox, oy, R2, R2);
                    R2=R2-step*2;
                    ox=ox+step;
                    oy=oy+step;
                }
                g2d.dispose();
            }
        };
		label1.setSize((int)atlaswidth, (int)atlasheight);
		label1.setBackground(new Color(0f,1f,0f,0.8f));
		label1.setLocation(0, 0);
		desktopPane.add(label1);

		//createmelabel();
		int labelw=60;
		int labelh=60;
		 melabel=new Mylabelinatlas(labelw,labelh);
		melabel.setLocation((int)atlaswidth/2-labelw/2, (int)atlasheight/2-labelh/2);
		desktopPane.add(melabel);

		//保存主角,敌方坦克，贴上敌方坦克标记
		enemylist=new ArrayList();
		TankInformation temptankinfo=new TankInformation();
		for(int j=0;j<alltanksinfo.size();j++)
		{
            temptankinfo=alltanksinfo.get(j);
            if(temptankinfo.getName().equals("player"))
            {
                playerinfo=temptankinfo;
            }else{
            	desktopPane.add(temptankinfo.getLabelinatalas());
            	enemylist.add(temptankinfo);
            }
		}
	}
}
